
package com.hlkj.pay.app.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.infrastructure.model.merchant.MerchantReportInfoDO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.app.merchant.IMerchantReportAmountAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.payment.MerchantProofDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.gateway.merchant.IMerchantReportGateway;
import com.hlkj.pay.infrastructure.model.merchant.MerchantReportAmountDO;
import com.hlkj.pay.service.merchant.IMerchantReportAmountService;
import com.hlkj.pay.service.merchant.IMerchantReportService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/12/25 14:18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MerchantReportAmountAppServiceImpl implements IMerchantReportAmountAppService {

    private final IMerchantReportService merchantReportService;

    private final IMerchantReportGateway merchantReportGateway;

    private final IMerchantReportAmountService merchantReportAmountService;

    @Value("${hlkj.pay.check:true}")
    private boolean check;

    @Value("${hlkj.domain}")
    private String domain;

    @Value("${hlkj.upload.hidden}")
    public String uploadHidden;

    @Value("${hlkj.upload.directory}")
    public String uploadDirectory;

    @Override
    public CommonResult<String> queryAllotProof(String code, boolean b) {
        log.info("queryAllotProof code:{},b:{}", code, b);
        try {
            MerchantReportAmountDO merchantReportAmountDO = merchantReportAmountService.queryAmountByCode(code);
            if (merchantReportAmountDO == null) {
                return CommonResult.error(CommonResultCode.DATA_NOT_EXIST);
            }
            if (MerchantEnum.REPORT_AMOUNT_PROCESS_STATUS.INIT.getCode().equals(merchantReportAmountDO.getProcessStatus())) {
                log.info("当前代付非执行成功,无非查看凭证:{}", code);
                return CommonResult.error(MerchantResultCode.MERCHANT_PAYMENT_NOT_SUCCESS);
            }
            if (StringUtils.hasText(merchantReportAmountDO.getProofPath())) {
                return CommonResult.success(domain + merchantReportAmountDO.getProofPath().replace(uploadDirectory, uploadHidden));
            }
            return processProof(merchantReportAmountDO);
        }
        catch (Exception e) {
            log.error("queryAllotProof error:{}", e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    CommonResult<String> processProof(MerchantReportAmountDO merchantReportAmountDO) {
        try {
            MerchantReportInfoDO merchantReportInfoDO = merchantReportService.detail(merchantReportAmountDO.getReportId());
            MerchantProofDto merchantProofDto = merchantReportGateway.queryAllotProof(merchantReportInfoDO, merchantReportAmountDO);
            if (!StringUtils.hasText(merchantProofDto.getProofPath()) && !StringUtils.hasText(merchantProofDto.getProofId())) {
                return CommonResult.success();
            }
            MerchantReportAmountDO updateMerchantPaymentDetailDO = new MerchantReportAmountDO();
            updateMerchantPaymentDetailDO.setProofPath(merchantProofDto.getProofPath());
            updateMerchantPaymentDetailDO.setId(merchantReportAmountDO.getId());
            merchantReportAmountService.updateReportAmount(updateMerchantPaymentDetailDO);
            merchantReportAmountDO.setProofPath(merchantProofDto.getProofPath());
            if (StringUtils.hasText(merchantProofDto.getProofPath())) {
                return CommonResult.success(domain + updateMerchantPaymentDetailDO.getProofPath().replace(uploadDirectory, uploadHidden));
            }
            else {
                CommonResult.error(CommonResultCode.SYSTEM_DOING);
            }
            return CommonResult.success();
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("代付凭啥生成失败:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }
}
